package com.business_clean.viewmodel.request

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.business_clean.app.App
import com.business_clean.app.network.NetUrl
import com.business_clean.data.mode.BaseDownLoadEntity
import com.business_clean.data.mode.attendance.AttendanceEntity
import com.business_clean.data.mode.attendance.MyAttendanceDetailEntity
import com.business_clean.data.mode.attendance.MyAttendanceEntity
import com.business_clean.data.mode.group.GroupMangerEntity
import me.hgj.mvvmhelper.base.BaseViewModel
import me.hgj.mvvmhelper.ext.rxHttpRequest
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

class AttendanceViewModel : BaseViewModel() {

    var attendanceEntity = MutableLiveData<AttendanceEntity>()
    var updateAttendance = MutableLiveData<Any>()
    var myAttendanceEntity = MutableLiveData<MyAttendanceEntity>()
    var groupMangerEntity = MutableLiveData<GroupMangerEntity>()
    var detailEntity = MutableLiveData<MyAttendanceDetailEntity>()


    var downLoadEntity = MutableLiveData<BaseDownLoadEntity>()


    //获取月统计  type 1是day 2 是月
    fun getAttendanceStat(type: Int, project_uuid: String? = "", group_uuid: String? = "", search_date: String? = "", is_list: String? = "", stat_status: String? = "") {
        rxHttpRequest {
            onRequest = {
                if (1 == type) {
                    attendanceEntity.value = RxHttp.get(NetUrl.GET_ATTENDANCE_DAY)
                        .add("project_uuid", project_uuid)
                        .add("group_uuid", group_uuid)
                        .add("search_date", search_date)
                        .add("stat_status", stat_status)
                        .add("is_list", is_list)
                        .toResponse<AttendanceEntity>().await()
                } else {
                    attendanceEntity.value = RxHttp.get(NetUrl.GET_ATTENDANCE_MONTH)
                        .add("project_uuid", project_uuid)
                        .add("group_uuid", group_uuid)
                        .add("search_month", search_date)
                        .add("is_list", is_list)
                        .add("stat_status", stat_status)
                        .toResponse<AttendanceEntity>().await()
                }
            }
        }
    }


    //更新统计的的状态
    fun updateAttendanceStatus(hashMap: HashMap<String, String>) {
        rxHttpRequest {
            onRequest = {
                updateAttendance.value = RxHttp.get(NetUrl.UPDATE_MY_ATTENDANCE)
                    .addAll(hashMap)
                    .toResponse<Any>().await()
            }
        }
    }


    /**
     * 获取小组信息
     */
    fun getGroupAll(project_uuid: String?) {
        rxHttpRequest {
            onRequest = {
                groupMangerEntity.value = RxHttp.get(NetUrl.GET_GROUP_ALL)
                    .add("project_uuid", if (!TextUtils.isEmpty(project_uuid)) project_uuid else App.appViewModelInstance.userInfo.value?.project?.uuid)
                    .toResponse<GroupMangerEntity>().await()
            }
        }
    }


    /**
     * 获取我的考勤
     */
    fun requestMyAttendance(hashMap: HashMap<String, String>) {
        rxHttpRequest {
            onRequest = {
                myAttendanceEntity.value = RxHttp.get(NetUrl.MY_ATTENDANCE)
                    .addAll(hashMap)
                    .toResponse<MyAttendanceEntity>().await()
            }
        }
    }

    /**
     * 获取当日考勤
     */
    fun requestMyAttendanceToday(stat_uuid: String? = "") {
        rxHttpRequest {
            onRequest = {
                detailEntity.value = RxHttp.get(NetUrl.MY_ATTENDANCE_DAYS)
                    .add("stat_uuid", stat_uuid)
                    .toResponse<MyAttendanceDetailEntity>().await()
            }
        }
    }


    /**
     * 操作类型 user档案 roster花名册 month_attendance月度考勤
     * 导出 excel
     */
    fun requestDownloadExcel(hashMap: HashMap<String, String>) {
        rxHttpRequest {
            onRequest = {
                downLoadEntity.value = RxHttp.get(NetUrl.DOWNLOAD_EXPORT_EXCEL)
                    .addAll(hashMap)
                    .toResponse<BaseDownLoadEntity>().await()
            }
        }
    }

}